package com.android.jblm.commandcentral;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.android.jblm.commandcentral.adapters.HeaderAdapter;
import com.android.jblm.commandcentral.objects.Header;

/**
 * Created by Brian 2014.
 */
public class HeaderListFragment extends ListFragment {

    private OnHeaderSelectedListener mCallBack;
    private  Header[] theHeaders = new Header[0];

    // Container Activity must implement this interface
    public interface OnHeaderSelectedListener {
        public void onHeaderSelected(int position);
        public Header[] getMyHeaders();
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        Log.w("HeaderListFragment", "Header List Created");
        theHeaders = mCallBack.getMyHeaders();
        Log.w("HeaderListFragment", "Header List 1 Title: " + theHeaders[0].getName() + "");
        // Populate list with our static array of myHeaders.
        setListAdapter(new HeaderAdapter(getActivity(), theHeaders));

        Log.w("HeaderListFragment", "Header List Adapter Size: " + this.getListAdapter().getCount() + "");


    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallBack = (OnHeaderSelectedListener) activity;

        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeaderSelectedListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallBack = null;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.w("HeaderListFragment", "Header List List Item Selected");
        mCallBack.onHeaderSelected(position);

        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);

    }

}
